package cluedo;

import java.util.ArrayList;
import java.util.List;

public class Board {

	final int width = 24;
	final int height = 29;

	
	
	private int playerCount;
	private List<Card> weapons;
	private List<Card> characters;
	private List<Card> roomCards;
	private List<Card> solution;
	private List<Player> players;
	
	private Tile[][] tiles;

	private List<Room> rooms;
	
	public Board(int players) {
		
		this.playerCount = players;
		weapons = new ArrayList<Card>();
		characters = new ArrayList<Card>();
		roomCards = new ArrayList<Card>();
		solution = new ArrayList<Card>();
		this.players = new ArrayList<Player>();
		
		initialiseRooms();
		
		initialiseItems();
		
		setupBoard();
		
		dealHands();
		
		
	}
	
	public boolean legalMove(int x, int y) {		
		
		return x<width-1&&y<height-1&&(tiles[x][y]==null||tiles[x][y].isWalkable());
		
	}
	
	public Tile getTile(int x, int y) {
		return tiles[x][y];
	}
	
	public List<Player> getPlayers() {
		return players;
	}
	
	private void setupBoard() {
		tiles = new Tile[width][height];
		
		for(Room r : rooms) {
			for(Tile t : r.getTiles()) 
				tiles[t.x][t.y] = t;
			for(Tile t : r.getDoors())
				tiles[t.x][t.y] = t;
		}
		
	}
	
	private void dealHands() {
		for(int p = 0; p<playerCount; p++) {
			players.add(new Player(p));
		}
		solution.add(drawCard(weapons));
		solution.add(drawCard(characters));
		solution.add(drawCard(roomCards));
		
		Card c = null;
		int i = 0;
		while((c = drawCard(weapons)) != null) {
			players.get(i%playerCount).addCard(c);
			i++;
		}
		i=0;
		while((c = drawCard(characters)) != null) {
			players.get(i%playerCount).addCard(c);
			i++;
		}
		i=0;
		while((c = drawCard(roomCards)) != null) {
			players.get(i%playerCount).addCard(c);
			i++;
		}
		
	}
	
	private void initialiseRooms() {
		rooms = new ArrayList<Room>();
		
		rooms.add(new Room("Kitchen",new int[][]{{6,22}}, new int[][]{
				{0,21},{1,21},{2,21},{3,21},{4,21},{5,21},
				{0,22},{1,22},{2,22},{3,22},{4,22},{5,22},
				{0,23},{1,23},{2,23},{3,23},{4,23},{5,23},{6,23},
				{0,24},{1,24},{2,24},{3,24},{4,24},{5,24},{6,24},
				{0,25},{1,25},{2,25},{3,25},{4,25},{5,25},{6,25},
				{0,26},{1,26},{2,26},{3,26},{4,26},{5,26},{6,26},
				{0,27},{1,27},{2,27},{3,27},{4,27},{5,27},{6,27},
				{0,28},{1,28},{2,28},{3,28},{4,28},{5,28},{6,28}
		}));

		rooms.add(new Room("Patio", new int[][]{{5,17},{5,11},{7,16},{7,12}}, new int[][]{
				{0,10},{1,10},{2,10},{3,10},
				{0,11},{1,11},{2,11},{3,11},{4,11},		  {6,11},{7,11},
				{0,12},{1,12},{2,12},{3,12},{4,12},{5,12},{6,12},
				{0,13},{1,13},{2,13},{3,13},{4,13},{5,13},{6,13},{7,13},
				{0,14},{1,14},{2,14},{3,14},{4,14},{5,14},{6,14},{7,14},
				{0,15},{1,15},{2,15},{3,15},{4,15},{5,15},{6,15},{7,15},
				{0,16},{1,16},{2,16},{3,16},{4,16},{5,16},{6,16},
				{0,17},{1,17},{2,17},{3,17},{4,17},		  {6,17},{7,17},
				{0,18},{1,18},{2,18},{3,18}
		}));

		rooms.add(new Room("Spa", new int[][]{{5,5}}, new int[][]{
				{0,0},{1,0},{2,0},{3,0},{4,0},{5,0},
				{0,1},{1,1},{2,1},{3,1},{4,1},{5,1},
				{0,2},{1,2},{2,2},{3,2},{4,2},{5,2},
				{0,3},{1,3},{2,3},{3,3},{4,3},{5,3},
				{0,4},{1,4},{2,4},{3,4},{4,4},{5,4},
				{0,5},{1,5},{2,5},{3,5},{4,5},
				{0,6},{1,6},{2,6},{3,6},{4,6},
				{0,7},{1,7},{2,7},{3,7},{4,7}

		}));

		rooms.add(new Room("Theatre", new int[][]{{10,7}}, new int[][]{
				{8,0},{9,0},{10,0},{11,0},{12,0},
				{8,1},{9,1},{10,1},{11,1},{12,1},
				{8,2},{9,2},{10,2},{11,2},{12,2},
				{8,3},{9,3},{10,3},{11,3},{12,3},
				{8,4},{9,4},{10,4},{11,4},{12,4},
				{8,5},{9,5},{10,5},{11,5},{12,5},
				{8,6},{9,6},{10,6},{11,6},{12,6},
				{8,7},{9,7}, 	   {11,7},{12,7}
		}));

		rooms.add(new Room("Living Room", new int[][]{{16,8}}, new int[][]{
				{14,0},{15,0},{16,0},{17,0},{18,0},{19,0},
				{14,1},{15,1},{16,1},{17,1},{18,1},{19,1},
				{14,2},{15,2},{16,2},{17,2},{18,2},{19,2},
				{14,3},{15,3},{16,3},{17,3},{18,3},{19,3},
				{14,4},{15,4},{16,4},{17,4},{18,4},{19,4},
				{14,5},{15,5},{16,5},{17,5},{18,5},{19,5},
				{14,6},{15,6},{16,6},{17,6},{18,6},{19,6},
				{14,7},{15,7},{16,7},{17,7},{18,7},{19,7},
					   {15,8} 		,{17,8}
		}));

		rooms.add(new Room("Observatory", new int[][]{{22,8}}, new int[][]{
				{22,0},{23,0},
				{22,1},{23,1},
				{22,2},{23,2},
				{22,3},{23,3},
				{22,4},{23,4},
				{22,5},{23,5},
				{22,6},{23,6},
				{22,7},{23,7},
					   {23,8}
		}));

		rooms.add(new Room("Hall", new int[][]{{19,13},{19,14},{22,11},{21,11}}, new int[][]{
				{19,11},{20,11}				   ,{23,11},
				{19,12},{20,12},{21,12},{22,12},{23,12},
						{20,13},{21,13},{22,13},{23,13},
						{20,14},{21,14},{22,14},{23,14},
				{19,15},{20,15},{21,15},{22,15},{23,15},
				{19,16},{20,16},{21,16},{22,16},{23,16},
				{19,17},{20,17},{21,17},{22,17},{23,17},
		}));

		rooms.add(new Room("Pool", new int[][]{}, new int [][]{}));
		rooms.add(new Room("Guest House", new int[][]{}, new int [][]{}));
		rooms.add(new Room("Dining Room", new int[][]{}, new int [][]{}));
		
		
	}

	private void initialiseItems(){
		weapons.add(new Weapon("Rope"));
		weapons.add(new Weapon("Candlestick"));
		weapons.add(new Weapon("Knife"));
		weapons.add(new Weapon("Pistol"));
		weapons.add(new Weapon("Baseball Bat"));
		weapons.add(new Weapon("Dumbbell"));
		weapons.add(new Weapon("Trophy"));
		weapons.add(new Weapon("Poison"));
		weapons.add(new Weapon("Axe"));

		characters.add(new Character("Kasandra Scarlett", 18, 28));
		characters.add(new Character("Jack Mustard", 7, 28));
		characters.add(new Character("Diane White", 0, 18));
		characters.add(new Character("Jacob Green", 0, 9));
		characters.add(new Character("Eleanor Peacock", 7, 0));
		characters.add(new Character("Victor Plum", 20, 0));
		
		roomCards.addAll(rooms);
	}
	
	private Card drawCard(List<Card> deck) {
		if(deck.size()==0) return null;
		Card card = null;
		if(deck.size()==1)
			card = deck.get(0);
		else {
			int rand = new java.util.Random().nextInt(deck.size()-1);
			card = deck.get(rand);
		}
		deck.remove(card);
		return card;
	}


}
